<template>
  <div v-if="bol">
    <hmNanBar
      path="/home/question"
      title="刷题"
      right="答题卡"
      @rightClick="rightEvent"
    ></hmNanBar>
    <div class="content" v-if="list.length && list[currIndex].detail">
      <div class="title">
        【{{ typeObj[list[currIndex].detail.type] }}】{{
          list[currIndex].detail.title
        }}
      </div>
      <van-tag
        class="tag"
        text-color="#B4B4BD"
        color="#f7f5f4"
        v-for="(item, index) in list[currIndex].detail.tag"
        :key="index"
        >{{ item }}</van-tag
      >
      <ul class="select" v-if="list[currIndex].detail.type == 1">
        <li
          :class="{
            active: str[index] === ans1,
            right:
              list[currIndex].answer &&
              str[index] === list[currIndex].answer.singleAnswer,
            error:
              list[currIndex].answer &&
              ans1 === str[index] &&
              ans1 !== list[currIndex].answer.singleAnswer
          }"
          class="item"
          v-for="(item, index) in list[currIndex].detail.option"
          :key="index"
          @click="liClick(index)"
        >
          {{ str[index] }}. {{ item }}
        </li>
      </ul>
      <ul class="select" v-else-if="list[currIndex].detail.type === 2">
        <li
          :class="{
            active: ans2.includes(str[index]),
            right:
              list[currIndex].answer &&
              list[currIndex].answer.multipleAnswer.includes(str[index])
          }"
          class="item"
          v-for="(item, index) in list[currIndex].detail.option"
          :key="index"
          @click="liClick2(index)"
        >
          {{ str[index] }}. {{ item }}
        </li>
      </ul>
      <div v-else>
        <van-field
          v-model="ans3"
          type="textarea"
          rows="6"
          @input="inputEnevt"
          :disabled="step > 1"
        ></van-field>
      </div>
      <div class="answer" v-if="step > 1">
        <div class="answer-title">答案解析</div>
        <div class="answer-right" v-if="list[currIndex].detail.type == 1">
          正确答案：{{ list[currIndex].answer.singleAnswer }}
        </div>
        <div class="answer-right" v-else-if="list[currIndex].detail.type === 2">
          正确答案：{{ list[currIndex].answer.multipleAnswer }}
        </div>
        <div class="other">
          <div class="o1">
            难度：{{ diffObj[list[currIndex].answer.difficulty] }}
          </div>
          <div class="o2">提交次数：{{ list[currIndex].answer.submitNum }}</div>
          <div class="o3">
            正确次数：{{ list[currIndex].answer.correctNum }}
          </div>
        </div>
        <div class="answer-txt">
          {{ list[currIndex].answer.answerAnalysis }}
        </div>
      </div>
    </div>
    <div class="footer">
      <div class="f1">
        <div class="iconfont">&#xe63c;</div>
        <div class="txt">收藏</div>
      </div>
      <div class="f1">
        <div class="iconfont">&#xe606;</div>
        <div class="txt">反馈</div>
      </div>
      <div class="f2">
        <span>{{ currIndex + 1 }}</span
        >/{{ list.length }}题
      </div>
      <van-button
        color="red"
        v-if="step < 2"
        :disabled="step < 1"
        @click="submit"
        >提交</van-button
      >
      <van-button color="red" v-else-if="step === 2" @click="next"
        >下一题</van-button
      >
      <van-button color="red" v-else>结束</van-button>
    </div>
    <van-popup v-model="showCard" position="top">
      <ul class="card-list">
        <li
          class="card-item"
          v-for="(item, index) in list"
          :key="index"
          :class="{
            right: item.answer && item.answer.isRight,
            error: item.answer && !item.answer.isRight
          }"
          @click="skip(index)"
        >
          {{ index + 1 }}
        </li>
        <li class="noUse"></li>
        <li class="noUse"></li>
        <li class="noUse"></li>
        <li class="noUse"></li>
        <li class="noUse"></li>
        <li class="noUse"></li>
        <li class="noUse"></li>
        <li class="noUse"></li>
      </ul>
    </van-popup>
  </div>
</template>

<script>
import {
  interviewQuestions,
  questionsSubmit,
  questionsId
} from '@/api/question.js'
export default {
  data () {
    return {
      id: this.$route.params.id,
      city: this.$route.params.city,
      list: [],
      currIndex: 0,
      typeObj: {
        1: '单选',
        2: '多选',
        3: '简答'
      },
      diffObj: {
        1: '简单',
        2: '一般',
        3: '困难'
      },
      ans1: '',
      ans2: [],
      ans3: '',
      str: 'ABCDEFGHI',
      step: 0,
      bol: true,
      showCard: false
    }
  },
  async created () {
    const res = await interviewQuestions({
      type: this.id,
      city: this.city
    })
    this.list = res.data.data
    // window.console.log(res)
  },
  methods: {
    // 答题卡
    rightEvent () {
      this.showCard = true
    },
    // 单选
    liClick (index) {
      this.ans1 = this.str[index]
      this.step = 1
    },
    // 多选
    liClick2 (index) {
      if (this.step < 2) {
        const _index = this.ans2.indexOf(this.str[index])
        if (_index !== -1) {
          this.ans2.splice(_index, 1)
        } else {
          this.ans2.push(this.str[index])
        }
        if (this.ans2.length === 0) {
          this.step = 0
        } else {
          this.step = 1
        }
        // window.console.log(this.ans2)
      }
    },
    // 简答
    inputEnevt () {
      if (this.ans3.length === 0) {
        this.step = 0
      } else {
        this.step = 1
      }
    },
    // 点击确定
    async submit () {
      this.$toast.loading({ dyration: 0 })
      const res = await questionsSubmit({
        id: this.list[this.currIndex].id,
        singleAnswer: this.ans1,
        multipleAnswer: this.ans2
      })
      console.log(res)
      this.$toast.clear()
      // 判断是不是最后一题
      if (this.currIndex === this.list.length - 1) {
        this.step = 3
      } else {
        this.step = 2
      }
      // 储存答案 存储当前点击过的题目答案与选择
      this.list[this.currIndex].ans1 = this.ans1
      this.list[this.currIndex].ans2 = this.ans2
      this.list[this.currIndex].ans3 = this.ans3
      this.list[this.currIndex].answer = res.data.data
      // window.console.log(this.list[this.currIndex].answer)
    },
    // 点击下一题
    async next () {
      this.currIndex++
      this.skip(this.currIndex)
    },
    async skip (index) {
      this.showCard = false
      this.currIndex = index
      this.ans1 = ''
      this.ans2 = []
      this.ans3 = ''
      this.step = 0
      // 有题目
      if (this.list[this.currIndex].detail) {
        // 有答案还原
        if (this.list[this.currIndex].answer) {
          this.ans1 = this.list[this.currIndex].ans1
          this.ans2 = this.list[this.currIndex].ans2
          this.ans3 = this.list[this.currIndex].ans3
          if (this.currIndex === this.list.length - 1) {
            this.step = 3
          } else {
            this.step = 2
          }
        } else {
          this.step = 0
        }
      } else {
        this.$toast.loading({ dyration: 0 })
        const res = await questionsId(this.list[this.currIndex].id)
        this.$toast.clear()
        this.list[this.currIndex].detail = res.data.data
        this.step = 0
      }
      // 静默刷新
      this.bol = false
      this.$nextTick(() => {
        this.bol = true
      })
    }
  }
}
</script>

<style lang="less" scoped>
.content {
  padding: 0 @p15;
  .title {
    padding-top: 25px;
    font-size: 16px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    text-align: justify;
    color: #181a39;
    line-height: 22px;
  }
  .tag {
    padding: 5px;
    margin-top: 10px;
    margin-bottom: 30px;
  }
  .select {
    padding-left: 20px;
    padding-right: 20px;
    .item {
      font-size: 16px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 500;
      text-align: justify;
      color: #181a39;
      line-height: 22px;
      padding: 15px;
      border: 1px solid #f7f4f5;
      margin-bottom: 16px;
    }
  }
  .active {
    border: 1px solid #000 !important;
  }
  .answer {
    padding-top: 20px;
    padding-bottom: 80px;
    .answer-title {
      font-size: 18px;
      font-family: PingFangSC, PingFangSC-Medium;
      font-weight: 500;
      text-align: left;
      color: #222222;
      line-height: 25px;
      letter-spacing: 0px;
      padding-top: 18px;
      padding-bottom: 18px;
    }
    .answer-right {
      font-size: 16px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 500;
      text-align: justify;
      color: #1dc779;
      line-height: 22px;
    }
    .other {
      margin-top: 20px;
      margin-bottom: 20px;
      width: 100%;
      height: 32px;
      background: #f7f4f5;
      border-radius: 4px;
      align-items: center;
      display: flex;
      font-size: 12px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      text-align: left;
      color: #545671;
      line-height: 12px;
      letter-spacing: 0px;
      .o1 {
        padding-right: 15px;
      }
      .o2 {
        padding-right: 15px;
      }
    }
    .answer-txt {
      font-size: 16px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      text-align: justify;
      color: #181a39;
      line-height: 22px;
    }
  }

  .card-list {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    height: 300px;
    overflow: auto;
    .card-item {
      width: 40px;
      height: 40px;
      text-align: center;
      line-height: 40px;
      margin: 10px;
      border-radius: 50%;
      border: 1px solid #ccc;
    }
    .noUse {
      width: 40px;
      text-align: center;
      line-height: 40px;
      border-radius: 50%;
    }
  }
}
.van-field {
  border: 1px solid #ccc;
  margin-top: 15px;
}
.footer {
  background: #ffffff;
  box-shadow: 0px -6px 8px 0px rgba(24, 26, 57, 0.04);
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 70px;
  padding: 0 @p15;
  display: flex;
  align-items: center;
  text-align: center;
  font-size: 12px;
  color: #ccc;
  .f1 {
    padding-right: 21px;
    .iconfont {
      font-size: 28px;
    }
  }
  .f2 {
    padding-left: 24px;
    font-size: 18px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    text-align: center;
    line-height: 31px;
    letter-spacing: 1px;
    padding-right: 34px;
    span {
      color: red;
      font-size: 24px;
    }
  }
  .van-button {
    width: 100px;
    height: 40px;
    border-radius: 7px;
    flex: 2;
    font-size: 16px;
  }
}
.right {
  background-color: rgb(86, 240, 119) !important;
}
.error {
  background-color: rgb(235, 82, 62) !important;
}
.card-list {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  height: 300px;
  overflow: auto;
  .card-item {
    font-size: 14px;
    width: 40px;
    height: 40px;
    text-align: center;
    line-height: 40px;
    margin: 10px;
    border-radius: 50%;
    border: 1px solid #ccc;
  }
  .noUse {
    width: 40px;
    text-align: center;
    line-height: 40px;
    border-radius: 50%;
  }
}
</style>
